import {
	Body,
	Controller,
	DefaultValuePipe,
	Delete,
	Get,
	HttpCode,
	HttpStatus,
	Param,
	ParseIntPipe,
	Patch,
	Post,
	Query,
	Request,
	UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiHeader, ApiOperation, ApiTags } from '@nestjs/swagger';
import { PermissionService } from './permission.service';
import { CreatePermissionDto } from './dto/create-permission.dto';
import { UpdatePermissionDto } from './dto/update-permission.dto';
import { AuthGuard } from '@nestjs/passport';
import { AuthPermission } from './permission.decorator';

@ApiBearerAuth() // 需要token
@UseGuards(AuthGuard('jwt'))
@ApiTags('权限管理')
@ApiHeader({
	name: 'authoriation',
	required: true,
	description: 'token',
})
@Controller({
	path: 'permission',
	version: '1',
})
export class PermissionController {
	constructor(private readonly permissionService: PermissionService) {}

	@ApiOperation({
		tags: ['创建权限'],
		description: '创建权限',
	})
	@Post()
	@HttpCode(HttpStatus.OK)
	create(@Request() request, @Body() createPermissionDto: CreatePermissionDto) {
		const { userId } = request.user;
		console.log(createPermissionDto);
		return this.permissionService.create(createPermissionDto, userId);
	}

	@ApiOperation({
		tags: ['查询所有权限'],
		description: '查询所有权限',
	})
	@Get()
	@AuthPermission('hello')
	findAll() {
		return this.permissionService.findAll();
	}

	@ApiOperation({
		tags: ['查询所有权限'],
		description: '查询所有权限',
	})
	@Get('/getPermissionList')
	findPermissionList(
		@Query('pageNum', new DefaultValuePipe(1), ParseIntPipe) pageNum: number,
		@Query('pageSize', new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
		@Query('keyword', new DefaultValuePipe('')) keyword: string,
	) {
		return this.permissionService.findListByPage({
			pageNum,
			pageSize,
			keyword,
		});
	}

	@ApiOperation({
		tags: ['查询单个权限'],
		description: '查询单个权限',
	})
	@Get(':id')
	findOne(@Param('id') id: string) {
		return this.permissionService.findOne(+id);
	}

	@ApiOperation({
		tags: ['更新权限'],
		description: '更新权限',
	})
	@Patch(':id')
	update(@Param('id') id: number, @Body() updatePermissionDto: UpdatePermissionDto) {
		return this.permissionService.update(+id, updatePermissionDto);
	}

	@ApiOperation({
		tags: ['删除权限'],
		description: '删除权限',
	})
	@Delete(':id')
	remove(@Param('id') id: string) {
		return this.permissionService.remove(+id);
	}
}
